/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.ihm.view;

import client.controller.VisitorsLayerController;
import common.model.Visitor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.swing.JPanel;

/**
 *
 * @author nicolas
 */
public class VisitorLayer2D extends JPanel implements VisitorsView, KeyListener {

    //Variable declarations
    private List<VisitorSprite2D> visitors = new LinkedList<VisitorSprite2D>();
    private AffineTransform originAffineTransform = null;
    private Set<Visitor> visitorsSet = new LinkedHashSet<Visitor>();
    private Visitor guide = null;
    private VisitorsLayerController controller;
    private final int circleSize = 150;

    public VisitorLayer2D() {
        super();
        addKeyListener(this);
    }

    public VisitorsLayerController getController() {
        return controller;
    }

    public void setController(VisitorsLayerController controller) {
        this.controller = controller;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g; //Better with Graphics2D (double buffering...)
        VisitorSprite2D visitorSprite2D = null;
        boolean guideFound = false;
        for (Visitor vs : new LinkedHashSet<Visitor>(visitorsSet)) {
            originAffineTransform = g2.getTransform();
            visitorSprite2D = new VisitorSprite2D(this, vs);
            g2.setColor(visitorSprite2D.getColor());
            g2.translate(vs.getX(), vs.getZ());
            g2.rotate(Math.toRadians(vs.getRotation()));
            g2.fill(visitorSprite2D.getShape());
            g2.setColor(Color.RED);
            if (vs.isGuide()) {
                g2.drawOval(-circleSize / 2, -circleSize / 2, circleSize, circleSize);
                guide = vs;
                guideFound = true;
            }
            g2.setTransform(originAffineTransform);
        }

        if (!guideFound) {
            guide = null;
        }
    }

    public void addVisitor(Visitor visitor) {
        this.visitorsSet.add(visitor);
        this.repaint();
    }

    public void updateVisitor(Visitor visitor) {
        this.visitorsSet.remove(visitor);
        this.visitorsSet.add(visitor);
        this.repaint();
    }

    public void deleteVisitor(Visitor visitor) {
        this.visitorsSet.remove(visitor);
        this.repaint();
    }

    public void keyTyped(KeyEvent arg0) {
        char key = arg0.getKeyChar();
        controller.computeKeyEvent(key);
    }

    public void keyPressed(KeyEvent arg0) {
    }

    public void keyReleased(KeyEvent arg0) {
    }

    public void setGuide(String userName, boolean guide) {
        Iterator<Visitor> it = visitorsSet.iterator();
        while (it.hasNext()) {
            Visitor v = it.next();
            if (v.getUserName().equals(userName)) {
                this.visitorsSet.remove(v);
                v.setGuide(guide);
                this.visitorsSet.add(v);
                break;
            }
        }
        this.repaint();
    }

    public Visitor getGuide() {
        return guide;
    }

    public int getCircleSize() {
        return circleSize;
    }
}
